﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.Framework
{
    /// <summary>
    /// 生成远程事件委托
    /// </summary>
    [Serializable]
    public class RemotingBundleEvenHandler : MarshalByRefObject
    {
        /// <summary>
        /// 本地事件
        /// </summary>
        public event BundleEvenHandler BundleEvenHandler;

        public override object InitializeLifetimeService()
        {
            // this object has to live "forever"
            return null;
        }

        /// <summary>
        /// 触发本地事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="context"></param>
        public void FireEvent(object sender, IBundleContext context)
        {
            if (this.BundleEvenHandler != null)
            {
                this.BundleEvenHandler(sender, context);
            }
        }

        /// <summary>
        /// 生成事件委托
        /// </summary>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static BundleEvenHandler CreateDelegate(BundleEvenHandler callback)
        {
            var proxy = new RemotingBundleEvenHandler();
            proxy.BundleEvenHandler += callback;

            return new BundleEvenHandler(proxy.FireEvent);
        }
    }
}
